package cn.pigeon.usermanager.controller;

import cn.pigeon.usermanager.pojo.User;
import cn.pigeon.usermanager.pojo.UserDetail;
import cn.pigeon.usermanager.pojo.UserView;
import cn.pigeon.usermanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login.do")
    public void login(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        User findUser = userService.findByNameAndPassword(user);
        if(findUser!=null){
            request.getSession().setAttribute("user",findUser);
            response.sendRedirect(request.getContextPath()+"/pages/main.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }

    @RequestMapping("/list.do")
    public ModelAndView list(UserView userView){
        List<UserView> userList = userService.findAll(userView);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList",userList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

    /**
     * 添加用户UI
     * @return
     */
    @RequestMapping("/addUI.do")
    public ModelAndView addUI(){
        ModelAndView modelAndView = new ModelAndView();
        UserView userView =  new UserView();
        userView.setId(-1);
        modelAndView.addObject("userView",userView);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }

    /**
     * 更新用户UI
     * @return
     */
    @RequestMapping("/updateUI/{id}.do")
    public ModelAndView updateUI(@PathVariable("id") int id){
        ModelAndView modelAndView = new ModelAndView();
        UserView userView =  new UserView();
        userView.setId(id);

        //查找并设置用户信息
        User user = userService.findByid(id);
        userView.setUsername(user.getUsername());
        userView.setPassword(user.getPassword());
        //查找并设置用户详情信息
        UserDetail userDetail = userService.findDetailByUserId(id);
        userView.setDid(userDetail.getId());
        userView.setAddress(userDetail.getAddress());
        userView.setQq(userDetail.getQq());

        modelAndView.addObject("userView",userView);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }

    /**
     * 新增和保存用户及详情
     * @param userView
     * @return
     */
    @RequestMapping("/save.do")
    public String save(UserView userView){
        if(userView.getId()==-1){
            //新增
            userService.add(userView);
        }else{
            //编辑
            userService.update(userView);
        }
        return "redirect:/user/list.do";
    }

    @RequestMapping("/delete/{id}.do")
    public String delete(@PathVariable("id") int id){
        userService.deleteById(id);
        return "redirect:/user/list.do";
    }
}
